//
//  TicketController.m
//  BigToBig
//
//  Created by guochuangkeshi on 16/4/26.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "TicketController.h"
#import "UnUseController.h"
#import "DidUseController.h"
#import "ExpiredController.h"
#import "UnUseControllerSP.h"
#import "DidUseControllerSP.h"
#import "ExpiredControllerSP.h"
#include "UnUseControllerZB.h"
#import "DidUseControllerZB.h"
#import "ExpiredControllerZB.h"
@interface TicketController ()<UIScrollViewDelegate>
{
    UIView *pageV;
    UIImageView *cornerV;
    UIButton *selectedBtn;
    UIButton *selectedBtn1;
    UIScrollView *scrollV;
    
    UIButton *unUseBtn;
    UIButton *didUseBtn;
    UIButton *expiredBtn;
    
    BOOL isMove; //手动滑动scrollView的时候不触发点击事件的滑动效果
}
@property(nonatomic,strong)UnUseController *unUseVc;
@property(nonatomic,strong)DidUseController *didUseVc;
@property(nonatomic,strong)ExpiredController *expiredVc;
@property(nonatomic ,strong)UnUseControllerSP *unUseVcSP;
@property(nonatomic ,strong)DidUseControllerSP *didUseVcSP;
@property(nonatomic ,strong)ExpiredControllerSP *expiredVcSP;
@property(nonatomic ,strong)UnUseControllerZB *unUseVcZB;
@property(nonatomic ,strong)DidUseControllerZB *didUseVcZB;
@property(nonatomic ,strong)ExpiredControllerZB *expiredVcZB;
@property(nonatomic ,strong)UIButton *luyanBtn;
@property(nonatomic ,strong)UIButton *supeibtn;
@property(nonatomic ,strong)UIButton *zhiboBtn;


//@property(nonatomic ,strong)UIButton *unUseBtn;

@end

@implementation TicketController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);


    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    isMove = NO ;

    [self addNavTitleView];
    
    [self addThreeBtn];
    
    [self addScrollView];
    
    [self setBackBar];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden=NO;
}

//- (void)setBackBar{
//    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
//    backBtn.frame = CGRectMake(0, 0, 44, 44);
//    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
//    [backBtn setImage:[UIImage imageNamed:@"圆角矩形-1-副本"] forState:UIControlStateNormal];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
//    [self.navigationItem setHidesBackButton:YES animated:YES];
//    
//}

- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
}

- (void)backController:(UIButton *)btn{
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController popViewControllerAnimated:YES];
}

-(UnUseController *)unUseVc{
    if (_unUseVc==nil) {
        _unUseVc = [[UnUseController alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _unUseVc;
}

- (DidUseController *)didUseVc{
    if (_didUseVc==nil) {
        _didUseVc=[[DidUseController alloc]initWithStyle:UITableViewStyleGrouped];;
    }
    return _didUseVc;
}

- (ExpiredController *)expiredVc{
    if (_expiredVc==nil) {
        _expiredVc=[[ExpiredController alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _expiredVc;
}

- (UnUseControllerSP *)unUseVcSP{
    
    if (_unUseVcSP==nil) {
        _unUseVcSP=[[UnUseControllerSP alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _unUseVcSP;
}
- (DidUseControllerSP *)didUseVcSP{
    
    if (_didUseVcSP==nil) {
        _didUseVcSP=[[DidUseControllerSP alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _didUseVcSP;
}

- (ExpiredControllerSP *)expiredVcSP{
    
    if (_expiredVcSP==nil) {
        _expiredVcSP=[[ExpiredControllerSP alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _expiredVcSP;
}
- (UnUseControllerZB *)unUseVcZB{
    
    if (_unUseVcZB==nil) {
        _unUseVcZB=[[UnUseControllerZB alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _unUseVcZB;
}
- (DidUseControllerZB *)didUseVcZB{
    
    if (_didUseVcZB==nil) {
        _didUseVcZB=[[DidUseControllerZB alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _didUseVcZB;
}
- (ExpiredControllerZB *)expiredVcZB{
    
    if (_expiredVcZB==nil) {
        _expiredVcZB=[[ExpiredControllerZB alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _expiredVcZB;
}
- (void)addNavTitleView{
    
    UIView *titleV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
    self.navigationItem.titleView = titleV;
    
    UIButton *luyanBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 20, 40, 20)];
    [luyanBtn setTitle:@"路演" forState:UIControlStateNormal];
    [luyanBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [luyanBtn setTitleColor:COLOR_RGBA(88, 165, 255, 1.0) forState:UIControlStateSelected];
    [luyanBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.luyanBtn = luyanBtn;
    [luyanBtn addTarget:self action:@selector(luyanClick:) forControlEvents:UIControlEventTouchUpInside];
    [titleV addSubview:luyanBtn];
    
    UIButton *supeiBtn=[[UIButton alloc]initWithFrame:CGRectMake(60, 20, 40, 20)];
    [supeiBtn setTitle:@"速配" forState:UIControlStateNormal];
    [supeiBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [supeiBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [supeiBtn setTitleColor:COLOR_RGBA(88, 165, 255, 1.0) forState:UIControlStateSelected];
    self.supeibtn = supeiBtn;
    [supeiBtn addTarget:self action:@selector(supeiClick:) forControlEvents:UIControlEventTouchUpInside];
    [titleV addSubview:supeiBtn];
    
    UIButton *zhiboBtn=[[UIButton alloc]initWithFrame:CGRectMake(120, 20, 40, 20)];
    [zhiboBtn setTitle:@"直播" forState:UIControlStateNormal];
    [zhiboBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [zhiboBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [zhiboBtn setTitleColor:COLOR_RGBA(88, 165, 255, 1.0) forState:UIControlStateSelected];
    self.zhiboBtn = zhiboBtn;
    [zhiboBtn addTarget:self action:@selector(zhiboClick:) forControlEvents:UIControlEventTouchUpInside];
    [titleV addSubview:zhiboBtn];
    
    pageV =[[UIView alloc]initWithFrame:CGRectMake(0, 43, 40, 2)];
    pageV.backgroundColor=COLOR_RGBA(88, 165, 255, 1.0);
    [titleV addSubview:pageV];
    
    if (selectedBtn==nil) {
        
        luyanBtn.selected = YES;
        
        [luyanBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        
        selectedBtn = luyanBtn;
    }
    
}



- (void)luyanClick:(UIButton *)btn{
    
    if (selectedBtn==btn) {
        return;
    }
    
    btn.selected=YES;
    
    selectedBtn.selected=NO;
    
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];

    [selectedBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    
    selectedBtn = btn;
    
    [UIView animateWithDuration:.3 animations:^{
        
        pageV.frame = CGRectMake(0, 43, 40, 2);
    }];
    

    [UIView animateWithDuration:.3 animations:^{
        
//        cornerV.frame = CGRectMake(5, 18, 12, 10);
        
        if (!isMove) {
            
            [scrollV setContentOffset:CGPointMake(0, 0)];
        }
        
    } completion:^(BOOL finished) {
        
        isMove = NO;
    }];

    
}

- (void)supeiClick:(UIButton *)btn{
    
    if (selectedBtn==btn) {
        return;
    }

    btn.selected=YES;
    
    selectedBtn.selected=NO;
    
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    
    [selectedBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self unUseClick:unUseBtn];
    selectedBtn = btn;
    
    [UIView animateWithDuration:.3 animations:^{
        pageV.frame = CGRectMake(60, 43, 40, 2);
    }];

    [UIView animateWithDuration:.3 animations:^{
        
//        cornerV.frame = CGRectMake(5+kScreenWidth, 18, 12, 10);
        
        if (!isMove) {
            
            [scrollV setContentOffset:CGPointMake(3*kScreenWidth, 0)];
        }
        
    } completion:^(BOOL finished) {
        
        isMove = NO;
    }];
    
}

- (void)zhiboClick:(UIButton *)btn{
    
    if (selectedBtn==btn) {
        return;
    }
    
    btn.selected=YES;
    
    selectedBtn.selected=NO;
    
    [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    
    [selectedBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    
    selectedBtn = btn;
    
    [UIView animateWithDuration:.3 animations:^{
        pageV.frame = CGRectMake(120, 43, 40, 2);
    }];
    
    [UIView animateWithDuration:.3 animations:^{
        
//        cornerV.frame = CGRectMake(5+2*kScreenWidth, 18, 12, 10);
        
        if (!isMove) {
            
            [scrollV setContentOffset:CGPointMake(6*kScreenWidth, 0)];
        }
        
    } completion:^(BOOL finished) {
        
        isMove = NO;
    }];

    
    
}


- (void)addThreeBtn{
    
    UIView *titleV = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth-160)/2, 10, 160, 20)];
    [self.view addSubview:titleV];
    
    
    unUseBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    [unUseBtn setTitle:@"未使用" forState:UIControlStateNormal];
    [unUseBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [unUseBtn setTitleColor:COLOR_RGBA(88, 165, 255, 1.0) forState:UIControlStateSelected];
    [unUseBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//    self.unUseBtn = unUseBtn;
    [unUseBtn addTarget:self action:@selector(unUseClick:) forControlEvents:UIControlEventTouchUpInside];
//    unUseBtn.userInteractionEnabled = NO;
    [titleV addSubview:unUseBtn];
    
    didUseBtn=[[UIButton alloc]initWithFrame:CGRectMake(60, 0, 40, 20)];
    [didUseBtn setTitle:@"已使用" forState:UIControlStateNormal];
    [didUseBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [didUseBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [didUseBtn setTitleColor:COLOR_RGBA(88, 165, 255, 1.0) forState:UIControlStateSelected];
    [didUseBtn addTarget:self action:@selector(didUseClick:) forControlEvents:UIControlEventTouchUpInside];

    [titleV addSubview:didUseBtn];
    
    expiredBtn=[[UIButton alloc]initWithFrame:CGRectMake(120, 0, 40, 20)];
    [expiredBtn setTitle:@"已过期" forState:UIControlStateNormal];
    [expiredBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [expiredBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [expiredBtn setTitleColor:COLOR_RGBA(88, 165, 255, 1.0) forState:UIControlStateSelected];
    [expiredBtn addTarget:self action:@selector(expiredClick:) forControlEvents:UIControlEventTouchUpInside];
    [titleV addSubview:expiredBtn];
    
    cornerV = [[UIImageView alloc]initWithFrame:CGRectMake(5, 18, 12, 10)];
    cornerV.image = [UIImage imageNamed:@"脚_05"];
    [titleV addSubview:cornerV];
    
    
    
    if (selectedBtn1==nil) {
        
        unUseBtn.selected = YES;
        
        [unUseBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        
        selectedBtn1 = unUseBtn;
    }
    
}

- (void)unUseClick:(UIButton *)btn{
    
    if (selectedBtn1==btn) {
        return;
    }
    
    btn.selected=YES;
    
    selectedBtn1.selected=NO;
    
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    
    [selectedBtn1.titleLabel setFont:[UIFont systemFontOfSize:12]];
    
    selectedBtn1 = btn;
        if (self.luyanBtn.selected == YES) {
    
            [UIView animateWithDuration:.3 animations:^{
                
                cornerV.frame = CGRectMake(5, 18, 12, 10);
                
                if (!isMove) {
                    
                    [scrollV setContentOffset:CGPointMake(0, 0)];
                }
                
            } completion:^(BOOL finished) {
                
                isMove = NO;
            }];

    
        }
        if (self.supeibtn.selected == YES) {
    
            [UIView animateWithDuration:.3 animations:^{
                
                cornerV.frame = CGRectMake(5, 18, 12, 10);
                
                if (!isMove) {
                    
                    [scrollV setContentOffset:CGPointMake(3*kScreenWidth, 0)];
                }
                
            } completion:^(BOOL finished) {
                
                isMove = NO;
            }];

    
        }
        if (self.zhiboBtn.selected == YES) {
    
            [UIView animateWithDuration:.3 animations:^{
                
                cornerV.frame = CGRectMake(5, 18, 12, 10);
                
                if (!isMove) {
                    
                    [scrollV setContentOffset:CGPointMake(6*kScreenWidth, 0)];
                }
                
            } completion:^(BOOL finished) {
                
                isMove = NO;
            }];

            
        }

}

- (void)didUseClick:(UIButton *)btn{
    
    if (selectedBtn1==btn) {
        return;
    }
    
    btn.selected=YES;
    
    selectedBtn1.selected=NO;
    
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    
    [selectedBtn1.titleLabel setFont:[UIFont systemFontOfSize:12]];
    
    selectedBtn1 = btn;
    

    if (self.luyanBtn.selected == YES) {
        
        [UIView animateWithDuration:.3 animations:^{
            
            cornerV.frame = CGRectMake(65, 18, 12, 10);
            
            if (!isMove) {
                
                [scrollV setContentOffset:CGPointMake(kScreenWidth, 0)];
            }
            
        } completion:^(BOOL finished) {
            
            isMove = NO;
        }];
        
        
    }
    if (self.supeibtn.selected == YES) {
        
        [UIView animateWithDuration:.3 animations:^{
            
            cornerV.frame = CGRectMake(65, 18, 12, 10);
            
            if (!isMove) {
                
                [scrollV setContentOffset:CGPointMake(4*kScreenWidth, 0)];
            }
            
        } completion:^(BOOL finished) {
            
            isMove = NO;
        }];
        
        
    }
    if (self.zhiboBtn.selected == YES) {
        
        [UIView animateWithDuration:.3 animations:^{
            
            cornerV.frame = CGRectMake(65, 18, 12, 10);

            if (!isMove) {
                
                [scrollV setContentOffset:CGPointMake(7*kScreenWidth, 0)];
            }
            
        } completion:^(BOOL finished) {
            
            isMove = NO;
        }];
        
        
    }


}

- (void)expiredClick:(UIButton *)btn{
    
    if (selectedBtn1==btn) {
        
        return;
    }
    
    btn.selected=YES;
    
    selectedBtn1.selected=NO;
    
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    
    [selectedBtn1.titleLabel setFont:[UIFont systemFontOfSize:12]];
    
    selectedBtn1 = btn;

    if (self.luyanBtn.selected == YES) {
        
        [UIView animateWithDuration:.3 animations:^{
            
            cornerV.frame = CGRectMake(125, 18, 12, 10);
            
            if (!isMove) {
                
                [scrollV setContentOffset:CGPointMake(2*kScreenWidth, 0)];
            }
            
        } completion:^(BOOL finished) {
            
            isMove = NO;
        }];
        
        
    }
    if (self.supeibtn.selected == YES) {
        
        [UIView animateWithDuration:.3 animations:^{
            
            cornerV.frame = CGRectMake(125, 18, 12, 10);
            
            if (!isMove) {
                
                [scrollV setContentOffset:CGPointMake(5*kScreenWidth, 0)];
            }
            
        } completion:^(BOOL finished) {
            
            isMove = NO;
        }];
        
        
    }
    if (self.zhiboBtn.selected == YES) {
        
        [UIView animateWithDuration:.3 animations:^{
            
            cornerV.frame = CGRectMake(125, 18, 12, 10);
            
            if (!isMove) {
                
                [scrollV setContentOffset:CGPointMake(8*kScreenWidth, 0)];
            }
            
        } completion:^(BOOL finished) {
            
            isMove = NO;
        }];
        
        
    }

}

- (void)addScrollView{
    
    scrollV=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, kScreenWidth, kScreenHeight-30-64)];
    scrollV.backgroundColor=COLOR_RGBA(245, 245, 245, 1.0);
    scrollV.contentSize = CGSizeMake(9*kScreenWidth, scrollV.height);
    scrollV.pagingEnabled = YES;
    scrollV.delegate = self;
    scrollV.bounces = NO;
    [self.view addSubview:scrollV];
    
    self.unUseVc.tableView.frame = CGRectMake(15, 0, kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_unUseVc.tableView];
    
    self.didUseVc.tableView.frame = CGRectMake(15+kScreenWidth, 0, kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_didUseVc.tableView];
    
    self.expiredVc.tableView.frame = CGRectMake(15+2*kScreenWidth, 0, kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_expiredVc.tableView];
    
    self.unUseVcSP.tableView.frame = CGRectMake(15+3*kScreenWidth, 0,kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_unUseVcSP.tableView];
    
    self.didUseVcSP.tableView.frame = CGRectMake(15+4*kScreenWidth, 0,kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_didUseVcSP.tableView];

    self.expiredVcSP.tableView.frame = CGRectMake(15+5*kScreenWidth, 0,kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_expiredVcSP.tableView];
    
    self.unUseVcZB.tableView.frame = CGRectMake(15+6*kScreenWidth, 0,kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_unUseVcZB.tableView];
    
    self.didUseVcZB.tableView.frame = CGRectMake(15+7*kScreenWidth, 0,kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_didUseVcZB.tableView];
    
    self.expiredVcZB.tableView.frame = CGRectMake(15+8*kScreenWidth, 0,kScreenWidth-30, scrollV.height);
    [scrollV addSubview:_expiredVcZB.tableView];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    isMove = YES;
    
    CGFloat offsetX = scrollView.contentOffset.x ;

    if ((offsetX<=kScreenWidth*0.5)||((offsetX>2.5*kScreenWidth&&offsetX<3.5*kScreenWidth))||(offsetX>5.5*kScreenWidth&&offsetX<6.5*kScreenWidth)) {
        
        [self unUseClick:unUseBtn];


    }else if ((offsetX>0.5*kScreenWidth&&offsetX<kScreenWidth*1.5)||((offsetX>3.5*kScreenWidth&&offsetX<4.5*kScreenWidth)||((offsetX>6.5*kScreenWidth&&offsetX<7.5*kScreenWidth)))){
    

        [self didUseClick:didUseBtn];

    }else if((offsetX >kScreenWidth*1.5&&offsetX<kScreenWidth*2.5)||(offsetX>4.5*kScreenWidth&&offsetX<5.5*kScreenWidth)||((offsetX>7.5*kScreenWidth&&offsetX<8.5*kScreenWidth))){
        
        [self expiredClick:expiredBtn];
    }
    
    
    if (offsetX>=0&&offsetX<kScreenWidth *3) {
        
        [self luyanClick:self.luyanBtn];

    }else if (offsetX>=kScreenWidth *3&&offsetX<kScreenWidth *6){
        
        [self supeiClick:self.supeibtn];
        
    }else if(offsetX>=kScreenWidth *6&&offsetX<kScreenWidth *8){
        
        [self zhiboClick:self.zhiboBtn];
        
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    isMove = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
